package com.ycsj.goldmedalnewconcept.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.activity.PayDetailActivity;
import com.ycsj.goldmedalnewconcept.adapter.LazyBaseFragment;
import com.ycsj.goldmedalnewconcept.bean.ClassManageinfoResponse;
import com.ycsj.goldmedalnewconcept.bean.ClassManageinfoResponse.ClassManageinfo;
import com.ycsj.goldmedalnewconcept.constant.Constant;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ClassInfoFragment extends LazyBaseFragment {
	private LinearLayout llLoading;
	private String user_id;
	private ListView lv_classlist;
	private List<ClassManageinfo> classlist;
	private ClassInfoAdapter mAdapter;
	private List<Integer> plist;

	int[] classIcons = { R.drawable.class1, R.drawable.class2,
			R.drawable.class3, R.drawable.class4, R.drawable.class5,
			R.drawable.class6, R.drawable.class7, R.drawable.class8,
			R.drawable.class9, R.drawable.class10, };

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_classinfo;
	}

	@Override
	public void lazyInit(View view, Bundle savedInstanceState) {
		user_id = (String) getArguments().getSerializable("user_id");

		// 加载控件
		llLoading = (LinearLayout) view.findViewById(R.id.ll_loading);
		lv_classlist = (ListView) view.findViewById(R.id.lv_classlist);

		classlist = new ArrayList<ClassManageinfo>();
		plist = new ArrayList<Integer>();
		plist.add(0);
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "29");
		builder.add("c", user_id);
		builder.add("role", "2");
		// http://pm.moregolden.com/ycsj_count/MessageCenter?type=29&c=13006314760&role=2
		Request request = new Request.Builder()
				.url(Constant.ycsj_count_MessageCenter).post(builder.build())
				.build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {

			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();
				String Json = arg0.body().string();
				ClassManageinfoResponse fromJson = gson.fromJson(Json,
						ClassManageinfoResponse.class);
				Log.e("test", Json);

				if (fromJson != null) {
					int size = 0;
					if (fromJson.class1 != null) {
						size = 1;
					}
					if (fromJson.class2 != null) {
						size = 2;
					}
					if (fromJson.class3 != null) {
						size = 3;
					}
					if (fromJson.class4 != null) {
						size = 4;
					}
					if (fromJson.class5 != null) {
						size = 5;
					}
					if (fromJson.class6 != null) {
						size = 6;
					}
					if (fromJson.class7 != null) {
						size = 7;
					}
					if (fromJson.class8 != null) {
						size = 8;
					}
					if (fromJson.class9 != null) {
						size = 9;
					}
					if (fromJson.class10 != null) {
						size = 10;
					}

					switch (size) {

					case 10:
						classlist.add(fromJson.class10);
					case 9:
						classlist.add(fromJson.class9);
					case 8:
						classlist.add(fromJson.class8);
					case 7:
						classlist.add(fromJson.class7);
					case 6:
						classlist.add(fromJson.class6);
					case 5:
						classlist.add(fromJson.class5);
					case 4:
						classlist.add(fromJson.class4);
					case 3:
						classlist.add(fromJson.class3);
					case 2:
						classlist.add(fromJson.class2);
					case 1:
						classlist.add(fromJson.class1);
						break;
					default:
						break;
					}

					try {
						getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								llLoading.setVisibility(llLoading.GONE);
								Log.e("test", "classlist" + classlist.size());
								mAdapter = new ClassInfoAdapter(getActivity());
								lv_classlist.setAdapter(mAdapter);

							}
						});
					} catch (Exception e) {
						Log.e("test", "错误");
					}
				}
			}
		});
	}

	class ViewHolder {
		public ImageView iv_img;
		public ImageView iv_pull;
		public TextView tv_name;
		public TextView tv_jiaofeibili;
		public TextView tv_chuqinlv;
		public TextView tv_beikeshu;
		public TextView tv_score;
		public TextView tv_detail;
		public RelativeLayout rl_dowmview;
		public RelativeLayout rl_pull;

	}

	class ClassInfoAdapter extends BaseAdapter {

		
		
		private LayoutInflater mInflater = null;

		private ClassInfoAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return classlist.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.item_lv_classinfo,
						null);

				holder.iv_img = (ImageView) convertView
						.findViewById(R.id.iv_img);
				holder.iv_pull = (ImageView) convertView
						.findViewById(R.id.iv_pull);

				holder.tv_name = (TextView) convertView
						.findViewById(R.id.tv_name);
				holder.tv_jiaofeibili = (TextView) convertView
						.findViewById(R.id.tv_jiaofeibili);
				holder.tv_chuqinlv = (TextView) convertView
						.findViewById(R.id.tv_chuqinlv);
				holder.tv_beikeshu = (TextView) convertView
						.findViewById(R.id.tv_beikeshu);
				holder.tv_score = (TextView) convertView
						.findViewById(R.id.tv_score);
				holder.tv_detail = (TextView) convertView
						.findViewById(R.id.tv_detail);
				holder.rl_dowmview = (RelativeLayout) convertView
						.findViewById(R.id.rl_dowmview);
				holder.rl_pull = (RelativeLayout) convertView
						.findViewById(R.id.rl_pull);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.rl_dowmview.setVisibility(holder.rl_dowmview.GONE);
			holder.iv_pull.setImageResource(R.drawable.down_pull);
			
			for (int i = 0; i < plist.size(); i++) {
				if(position==plist.get(i)){
					holder.rl_dowmview.setVisibility(holder.rl_dowmview.VISIBLE);
					holder.iv_pull.setImageResource(R.drawable.up_pull);
				}
			}

			Picasso.with(getActivity())
					.load(classIcons[Integer.parseInt(classlist.get(position).icon) - 1])
					.error(R.drawable.header).into(holder.iv_img);

			holder.tv_name.setText(classlist.get(position).name);
			int total = (Integer.parseInt(classlist.get(position).payyes))
					+ (Integer.parseInt(classlist.get(position).payno));
			holder.tv_jiaofeibili.setText("缴费比例："
					+ classlist.get(position).payyes + "/" + total);
			holder.tv_chuqinlv.setText("学生出勤率："
					+ classlist.get(position).averageAttendance);
			holder.tv_beikeshu.setText("备课数："
					+ classlist.get(position).averageSelef);
			holder.tv_score.setText("平均成绩："
					+ classlist.get(position).averageExam);

			holder.rl_pull.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int k = 0;
					for (int i = 0; i < plist.size(); i++) {
						if(position==plist.get(i)){
							holder.rl_dowmview.setVisibility(holder.rl_dowmview.GONE);
							holder.iv_pull.setImageResource(R.drawable.down_pull);
							plist.remove(i);
							k = 1;
							
						}
					}
					
					if(k!=1){
						holder.rl_dowmview.setVisibility(holder.rl_dowmview.VISIBLE);
						holder.iv_pull.setImageResource(R.drawable.up_pull);
						plist.add(position);
					}
					
				}
			});

			holder.tv_detail.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					
					Intent intent = new Intent(getActivity(),PayDetailActivity.class);
					intent.putExtra("classnum", classlist.get(position).num+"");
					intent.putExtra("user_id", user_id);
					getActivity().startActivity(intent);
				}
			});


			return convertView;
		}
	}
}
